package com.essc.controller;


import com.essc.pojo.Goods;
import com.essc.service.GoodsService;
import com.essc.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.FileNotFoundException;

@CrossOrigin
@Controller
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    GoodsService goodsService;

    @GetMapping("/findGoodsInfoByPage/{pageNum}/{pageSize}")
    @ResponseBody
    public JsonResult findGoodsInfoByPage
            (@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {
        return goodsService.findGoodsInfoByPage(pageNum, pageSize);
    }

    //查询商品条数
    @GetMapping("/goodsNum")
    @ResponseBody
    public JsonResult goodsNum() {
        return goodsService.goodsNum();
    }

    //根据商品类别查询所有商品并分页
    @GetMapping("/findGoodsInfoByPageAndCid/{cid}/{pageNum}/{pageSize}")
    @ResponseBody
    public JsonResult findGoodsInfoByPageAndCid
    (@PathVariable("cid") Integer cid,
     @PathVariable("pageNum") Integer pageNum,
     @PathVariable("pageSize") Integer pageSize) {
        return goodsService.findGoodsInfoByPageAndCid(cid, pageNum, pageSize);
    }

    @GetMapping("/goodsNumByCid/{cid}")
    @ResponseBody
    public JsonResult goodsNumByCid(@PathVariable("cid") int cid) {

        return goodsService.goodsNumByCid(cid);
    }


    //    处理文件上传
    @RequestMapping("/upload")
    @ResponseBody
    public JsonResult uploadImage(@RequestParam(value = "file1") MultipartFile file, @RequestParam(value = "username") String username, HttpSession session) throws FileNotFoundException {
        return goodsService.uploadImage(file, username, session);
    }


    //添加商品
    @RequestMapping("/addGood")
    @ResponseBody
    public JsonResult addGoodPage(@RequestBody Goods good) {
        return goodsService.addGood(good);
    }

    @GetMapping(value = "/findGoodById/{gid}")
    @ResponseBody
    public JsonResult findGoodById(@PathVariable("gid") Integer gid) {
        return goodsService.findGoodById(gid);
    }

    //根据用户信息查找其发布的商品
    @GetMapping("/findGoodByUid/{uid}")
    @ResponseBody
    public JsonResult findGoodByUid(@PathVariable("uid") int uid) {
        return goodsService.findGoodByUid(uid);
    }


    //模糊查询商品
    @GetMapping("/findGoodsBySelect/{select}/{pageNum}/{pageSize}")
    @ResponseBody
    public JsonResult findGoodsBySelect(@PathVariable("select") String select,
                                        @PathVariable("pageNum") Integer pageNum,
                                        @PathVariable("pageSize") Integer pageSize) {
        return goodsService.findGoodsBySelect(select, pageNum, pageSize);
    }
}
